package com.yohann.boot.common.enums;

import lombok.Getter;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Yeung Wah
 * @since 2021/9/17
 */
@Getter
public enum TestEnum2 implements TranslationEnum, ValidEnum {
    /**
     * test enums
     */
    TEST_1(3, "test1"),
    TEST_2(4, "test2"),
    ;

    private Integer code;
    private String value;

    TestEnum2(Integer code, String value) {
        this.code = code;
        this.value = value;
    }

    /**
     * 静态缓存枚举加速
     */
    private static final Map<Object, String> VALUE_MAP = new HashMap<>(TestEnum2.values().length * 4 / 3);

    static {
        for (TestEnum2 value : TestEnum2.values()) {
            VALUE_MAP.put(value.code, value.value);
        }
    }

    @Override
    public Object getValue(Object code) {
        return VALUE_MAP.get(code);
    }

    @Override
    public boolean valid(Object o) {
        for (TestEnum2 testEnum : TestEnum2.values()) {
            if (testEnum.code.equals(o)) {
                return true;
            }
        }

        return false;
    }
}